package com.ivan.toolkit.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import lombok.Getter;

public class Applications {
	
	public static final String APPLICATION_NAME = "Applications.name";
	public static final String APPLICATION_HOME = "Applications.home";
	public static final String APPLICATION_VERSION = "Applications.version";

	private static Properties properties = null;
	
	@Getter
	private static Any any = Any.of();
	
	static {
		
		initialize();
		
	}
	
	public static void initialize() {
		
		if(properties == null) {
			
			try {
				
				ClassLoader loader = Thread.currentThread().getContextClassLoader();
				InputStream is = loader.getResourceAsStream("application.properties");
					
				if(is == null) {
					System.err.println("Config file application.properties not found.");				
				} else {
					Properties p = new Properties();
					p.load(is);
					properties = p;
					System.out.println("Applications is ready.");
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public static String getProperty(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}
	
	public static String getName() {
		return getProperty(Applications.APPLICATION_NAME, "UNSET");
	}
	
	public static File getHome() {
		String home = properties.getProperty(Applications.APPLICATION_HOME);
		if(home == null) {
			home = "." + getName();
		}
		return new File(System.getProperty("user.dir"), home);
	}
	
	public static String getVersion() {
		return properties.getProperty(Applications.APPLICATION_VERSION);
	}
	
}
